package com.uenpay.im.ui.old.activity.company;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.im.R;

import java.util.ArrayList;


/**
 * @author SL
 * @created 2016-2-22 13:02:55
 * @copyRight http://www.qiyunxin.com
 */
public class DepartmentsAdapter extends BaseAdapter {

	private ArrayList<DepartmentEntity> mArrayList = null;
	private LayoutInflater mLayoutInflater = null;
	private boolean is_choose = false;

	public DepartmentsAdapter(Context _Context,
			ArrayList<DepartmentEntity> _ArrayList,boolean _is_choose) {
		is_choose = _is_choose;
		mLayoutInflater = LayoutInflater.from(_Context);
		mArrayList = _ArrayList;
	}

	@Override
	public int getCount() {
		return mArrayList == null ? 0 : mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrayList == null ? null : mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		DepartmentViewHodler mDepartmentViewHodler = null;
		final DepartmentEntity mDepartmentEntity = mArrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(R.layout.item_departments_layout,
					null);
			mDepartmentViewHodler = new DepartmentViewHodler();
			mDepartmentViewHodler.nameTv = (TextView) arg1
					.findViewById(R.id.name_tv);
			mDepartmentViewHodler.cust_count_tv = (TextView) arg1
					.findViewById(R.id.cust_count_tv);
			mDepartmentViewHodler.iv = (ImageView) arg1.findViewById(R.id.iv);
			mDepartmentViewHodler.selecetd_iv = (ImageView) arg1
					.findViewById(R.id.selecetd_iv);
			arg1.setTag(mDepartmentViewHodler);
		} else {
			mDepartmentViewHodler = (DepartmentViewHodler) arg1.getTag();
		}
		mDepartmentViewHodler.nameTv.setText(mDepartmentEntity.departname);
		mDepartmentViewHodler.cust_count_tv.setText(mDepartmentEntity.custnum
				+ "");
		if (mDepartmentEntity.isleaf == 1) {
			mDepartmentViewHodler.iv.setVisibility(View.INVISIBLE);
		} else {
			mDepartmentViewHodler.iv.setVisibility(View.VISIBLE);
		}

		if(is_choose){
			mDepartmentViewHodler.selecetd_iv.setVisibility(View.VISIBLE);
		}else{
			mDepartmentViewHodler.selecetd_iv.setVisibility(View.GONE);
		}
		
		if (mDepartmentEntity.is_selected) {
			mDepartmentViewHodler.selecetd_iv
					.setImageResource(R.drawable.icon_checked);
		} else {
			mDepartmentViewHodler.selecetd_iv
					.setImageResource(R.drawable.icon_uncheck);
		}

		mDepartmentViewHodler.selecetd_iv
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						updateSelectedDepart(mDepartmentEntity);
					}
				});
		return arg1;
	}

	private class DepartmentViewHodler {
		private TextView nameTv, cust_count_tv;
		private ImageView iv, selecetd_iv;
	}

	/**
	 * @param mDepartmentEntity
	 */
	public void updateSelectedDepart(DepartmentEntity mDepartmentEntity) {
		for (int i = 0, size = mArrayList.size(); i < size; i++) {
			if (mDepartmentEntity.departid == mArrayList.get(i).departid
					&& mDepartmentEntity.entid == mArrayList.get(i).entid) {
				mArrayList.get(i).is_selected = true;
			} else {
				mArrayList.get(i).is_selected = false;
			}
		}
		notifyDataSetChanged();
	}

	/**
	 * @return
	 */
	public DepartmentEntity getSelectedDepart() {
		DepartmentEntity mDepartmentEntity = null;
		for (int i = 0, size = mArrayList.size(); i < size; i++) {
			if (mArrayList.get(i).is_selected) {
				mDepartmentEntity = mArrayList.get(i);
				break;
			}
		}
		return mDepartmentEntity;
	}
}
